﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    /// <summary>
    /// 单日账户交易合约(那些小计的合约信息也直接在这里面写入)当Account为-1时为总数
    /// </summary>
    public class ServiceAccountDayTradeInstrument : Benefit.Interface.Day.IAccountDayTradeInstrument
    {
        Benefit.DB.DBManager db = null;
        public ServiceAccountDayTradeInstrument(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.AccountDayTradeInstrument where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.AccountDayTradeInstrument c in query)
            {
                db.AccountDayTradeInstrument.Remove(c);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 保存账号日变化情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitDayAccountInstrumentChange(int tradeHistoryId, string pdate)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            Interface.Sys.ITradeHistory th = new Sys.ServiceTradeHistory(db);
            Interface.ServerData.IT_TradeDetail_History tth = new ServerData.ServiceT_TradeDetail_History(db);
            Interface.Sys.IAccount _account = new Sys.ServiceAccount(db);
            Interface.Sys.IInstrument iinstr = new Sys.ServiceInstrument(db);
            Interface.ServerData.IT_PositionDetail_History tth2 = new ServerData.ServiceT_PositionDetail_History(db);
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {
 
                List<string> instruments = tth.GetAccountDayInstrument(pdate, account.Id);
                foreach (string instrument in instruments)
                {
                    #region i为0是多，i为1是空
                    for (int i = 0; i < 2; i++)
                    {
                        Models.Day.AccountDayTradeInstrument change = new Models.Day.AccountDayTradeInstrument();
               
                        change.AccountId = _account.GetAccountId(account.LoginAccount);
                        change.TradeHistoryId = tradeHistoryId;
                        change.Direction = i;
                        change.BillCount = tth.GetDayInstrumentBillCount(pdate, account.Id, instrument, change.Direction.ToString());
                        change.Free = tth.GetDayInstrumentCommission(pdate, account.Id, instrument, change.Direction.ToString());
    
                        change.InstrumentId = iinstr.GetInstrumentId(instrument);
                        change.LoseCount = tth.GetDayInstrumentLoseCount(pdate, account.Id, instrument, change.Direction.ToString());
                        change.Profit = tth.GetDayInstrumentCloseProfit(pdate, account.Id, instrument, change.Direction.ToString());
                        change.DayCount = change.Profit - change.Free;
                        change.SumLost = tth.GetDayInstrumentAllLose(pdate, account.Id, instrument, change.Direction.ToString());
                        change.SumWin = tth.GetDayInstrumentAllWin(pdate, account.Id, instrument, change.Direction.ToString());

                        change.UsedMargin = tth2.GetAccountInstrument(account.Id, th.GetPdateFromSettlementId(tradeHistoryId), instrument);
                        change.WinCount = tth.GetDayInstrumentWinCount(pdate, account.Id, instrument, change.Direction.ToString());
                        db.AccountDayTradeInstrument.Add(change);
                    }
                    #endregion
                }
            }
            db.SaveChanges();
        }


        public List<Models.Day.AccountDayTradeInstrument> GetAccountDayTradeInstruments(int accountid, int tradeHistoryId)
        {
            List<Models.Day.AccountDayTradeInstrument> instruments = new List<Models.Day.AccountDayTradeInstrument>();
            var query = db.AccountDayTradeInstrument.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                instruments = query.ToList();
            }
            return instruments;
        }
    }
}